﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class TerrainDepthOnlyPass : ScriptableRenderPass
{
    const string m_ProfilerTag = "Terrain Depth Prepass";
    ProfilingSampler m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
    
    private RenderTargetIdentifier depthAttachmentIdentifier;
    private RenderTexture hizTexture;
    private int curDrawIndex;
    private Matrix4x4 _VPPrev;

    public static Action<ScriptableRenderContext, Camera, RenderTexture, int, Matrix4x4> ExecuteAction;

    public TerrainDepthOnlyPass(RenderPassEvent evt)
    {
        renderPassEvent = evt;
        curDrawIndex = 0;
    }

    public void Setup(RenderTargetIdentifier depthAttachmentIdentifier, RenderTexture hizTexture, Matrix4x4 vpPrevFrame)
    {
        this.depthAttachmentIdentifier = depthAttachmentIdentifier;
        this.hizTexture = hizTexture;
        this._VPPrev = vpPrevFrame;
    }

    public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    {
        ConfigureTarget(depthAttachmentIdentifier);
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        if (ExecuteAction == null && hizTexture != null)
            return;

        ExecuteAction?.Invoke(context, renderingData.cameraData.camera, hizTexture, curDrawIndex, _VPPrev);

        curDrawIndex = (curDrawIndex + 1) % 2;
    }

}
